use std::io::{self, Write};
use std::path::Path;
use walkdir::WalkDir;

fn load_files(path: &str, keyword: &str) {
    let root = Path::new(path);
    let keyword_lower = keyword.to_lowercase();

    for entry in WalkDir::new(root).into_iter().filter_map(|e| e.ok()) {
        let path = entry.path();

        // Show all if keyword is empty, otherwise check for match
        let should_show = keyword.is_empty() || 
            path.file_name()
                .and_then(|n| n.to_str())
                .map(|s| s.to_lowercase().contains(&keyword_lower))
                .unwrap_or(false);

        if should_show {
            println!(
                "Found {}: {}",
                if path.is_dir() { "dir" } else { "file" },
                path.display()
            );
        }
    }
}

fn main() {
    let mut path_input = String::new();
    let mut keyword_input = String::new();

    // Get path input
    print!("Enter path to search: ");
    io::stdout().flush().unwrap();
    io::stdin().read_line(&mut path_input).unwrap();
    let path = path_input.trim();

    // Get keyword input (optional)
    print!("Enter search keyword (leave empty to show all): ");
    io::stdout().flush().unwrap();
    io::stdin().read_line(&mut keyword_input).unwrap();
    let keyword = keyword_input.trim();

    load_files(path, keyword);
}